<template>
  <div>
    <a-layout id="components-layout-demo-fixed-sider" style="min-height: 100vh">
      <f-sider></f-sider>
      <a-layout>
        <f-header></f-header>
        <a-layout-content
          :style="{ margin: '24px 16px 0', overflow: 'initial' }"
        >
          <div :style="{ padding: '24px', background: '#fff' }">
            <a-row type="flex" align="middle">
              <a-col :span="4">资源标题：</a-col>
              <a-col :span="8">
                <a-input
                  v-model="resources_title"
                  placeholder="请输入资源标题"
                />
              </a-col>
            </a-row>
            <a-row class="mt1rem" type="flex" align="middle">
              <a-col :span="4">资源分类：</a-col>
              <a-col :span="12">
                <a-select
                  :defaultValue="resources_cate_title"
                  style="width: 150px"
                  @change="resourcesCateChange"
                >
                  <a-select-option
                    v-for="(items, index) in ResourcesCate"
                    :key="index"
                    :value="items.resources_catid"
                    >{{ items.resources_cate_title }}</a-select-option
                  >
                </a-select>
              </a-col>
            </a-row>
            <a-row class="mt1rem" type="flex" align="middle">
              <a-col :span="4">资源简介：</a-col>
              <a-col :span="8">
                <a-textarea
                  v-model="resources_introduce"
                  placeholder="请输入资源简介"
                  :auto-size="{ minRows: 3, maxRows: 5 }"
                />
              </a-col>
            </a-row>
            <a-row class="mt1rem" type="flex">
              <a-col :span="4">下载地址：</a-col>
              <a-col :span="8">
                <a-textarea
                  v-model="resources_link"
                  placeholder="请填写下载地址"
                  autoSize
                />
              </a-col>
            </a-row>
            <div class="btn">
              <a-button type="primary" :loading="iconLoading" @click="save"
                >添加</a-button
              >
              <a-button class="ml1rem" type="primary" @click="back"
                >返回</a-button
              >
            </div>
          </div>
        </a-layout-content>
        <f-footer></f-footer>
      </a-layout>
    </a-layout>
  </div>
</template>
<script>
import sider from "../../components/layout/Sider.vue";
import header from "../../components/layout/Header.vue";
import footer from "../../components/layout/Footer.vue";
export default {
  data() {
    return {
      iconLoading: false,
      loading: false,
      resources_title: "",
      resources_introduce: "",
      resources_catid: 0,
      resources_link: "",
      ResourcesCate: [],
      resources_cate_title: "请选择资源分类",
    };
  },
  components: {
    "f-header": header,
    "f-sider": sider,
    "f-footer": footer,
  },
  mounted() {
    this.$parent.tokenverify();
    this.getdata();
  },
  methods: {
    getdata() {
      this.setsiders();
      this.getResourcesCate();
    },
    //获取资源分类
    getResourcesCate: function () {
      const url = "/Admin/Resources/getAllResourcesCate";
      const paramObj = {
        token: localStorage.getItem("token"),
      };
      this.$server.sl("GET", url, paramObj).then((res) => {
        if (res.code == 1) {
          this.ResourcesCate = res.data;
        } else if (res.code == 4001) {
          this.$router.push("/Auth/login");
          this.$message.warning("凭证失效，请重新登陆");
        } else {
          this.$message.warning("获取文章分类失败，请刷新重试");
        }
      });
    },
    //提交内容
    save() {
      const url = "/Admin/Resources/addResources";
      const paramObj = {
        resources_title: this.resources_title,
        resources_introduce: this.resources_introduce,
        resources_link: this.resources_link,
        resources_catid: this.resources_catid,
        token: localStorage.getItem("token"),
      };
      this.$server.sl("POST", url, paramObj).then((res) => {
        if (res.code == 1) {
          this.$message.success("添加成功");
          this.$router.go(-1);
        } else if (res.code == 4001) {
          this.$router.push("/Auth/login");
          this.$message.warning("凭证失效，请重新登陆");
        } else {
          this.$message.warning("添加失败，请刷新重试");
        }
      });
    },
    //返回上一页
    back() {
      this.$router.go(-1);
    },
    //资源分类选择
    resourcesCateChange(value) {
      this.resources_catid = value;
    },
    //菜单选中设置
    setsiders() {
      this.$store.commit("updateselectedKeys", "11");
    },
  },
};
</script>
<style scoped>
.mt1rem {
  margin-top: 1rem;
}
.ml1rem {
  margin-left: 1rem;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
.btn {
  margin-top: 10px;
}
</style>
